@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

echo ========================================
echo LimeReader 安装包制作工具
echo 影谷长图处理大师 v2.7.2
echo ========================================
echo.

:: 检查编译输出是否存在
if not exist "build\bin\Release\LimeReader.exe" (
    echo [错误] 找不到编译后的可执行文件！
    echo 请先编译项目：build.bat
    pause
    exit /b 1
)

:: 设置 Qt 路径（根据你的 Qt 安装路径调整）
set "QT_DIR=C:\Qt\6.9.2\mingw_64"
if not exist "%QT_DIR%\bin\windeployqt.exe" (
    echo [警告] 找不到 windeployqt.exe
    echo 将尝试使用系统 PATH 中的 windeployqt
    set "WINDEPLOYQT=windeployqt"
) else (
    set "WINDEPLOYQT=%QT_DIR%\bin\windeployqt.exe"
    set "PATH=%QT_DIR%\bin;%PATH%"
)

:: 创建临时部署目录
set "DEPLOY_DIR=build\deploy_temp"
if exist "%DEPLOY_DIR%" (
    echo [清理] 删除旧的部署目录...
    rmdir /s /q "%DEPLOY_DIR%"
)
mkdir "%DEPLOY_DIR%"

echo.
echo [步骤 1] 复制主程序...
copy "build\bin\Release\LimeReader.exe" "%DEPLOY_DIR%\" >nul
if errorlevel 1 (
    echo [错误] 复制主程序失败！
    pause
    exit /b 1
)
echo [完成] 主程序已复制

echo.
echo [步骤 2] 运行 windeployqt 收集 Qt 依赖...
"%WINDEPLOYQT%" --release --no-compiler-runtime --no-system-d3d-compiler --no-opengl-sw "%DEPLOY_DIR%\LimeReader.exe"
if errorlevel 1 (
    echo [警告] windeployqt 执行失败或未找到，将手动复制必要文件
)
echo [完成] Qt 依赖已收集

echo.
echo [步骤 3] 复制 MinGW 运行时库...
if exist "%QT_DIR%\bin\libgcc_s_seh-1.dll" (
    copy "%QT_DIR%\bin\libgcc_s_seh-1.dll" "%DEPLOY_DIR%\" >nul
)
if exist "%QT_DIR%\bin\libstdc++-6.dll" (
    copy "%QT_DIR%\bin\libstdc++-6.dll" "%DEPLOY_DIR%\" >nul
)
if exist "%QT_DIR%\bin\libwinpthread-1.dll" (
    copy "%QT_DIR%\bin\libwinpthread-1.dll" "%DEPLOY_DIR%\" >nul
)
echo [完成] MinGW 运行时库已复制

echo.
echo [步骤 4] 复制 Poppler PDF 库...
if exist "mingw64-poppler\bin\libpoppler-153.dll" (
    copy "mingw64-poppler\bin\libpoppler-153.dll" "%DEPLOY_DIR%\" >nul
)
if exist "mingw64-poppler\bin\libpoppler-cpp-2.dll" (
    copy "mingw64-poppler\bin\libpoppler-cpp-2.dll" "%DEPLOY_DIR%\" >nul
)
if exist "mingw64-poppler\bin\libpoppler-glib-8.dll" (
    copy "mingw64-poppler\bin\libpoppler-glib-8.dll" "%DEPLOY_DIR%\" >nul
)
echo [完成] Poppler 库已复制

echo.
echo [步骤 5] 复制翻译文件...
if exist "translations" (
    if not exist "%DEPLOY_DIR%\translations" mkdir "%DEPLOY_DIR%\translations"
    xcopy /y /s /q "translations\*.qm" "%DEPLOY_DIR%\translations\" >nul 2>&1
)
echo [完成] 翻译文件已复制

echo.
echo [步骤 6] 复制资源文件...
if exist "icons" (
    xcopy /y /s /q /i "icons" "%DEPLOY_DIR%\icons\" >nul 2>&1
)
if exist "images" (
    xcopy /y /s /q /i "images" "%DEPLOY_DIR%\images\" >nul 2>&1
)
echo [完成] 资源文件已复制

echo.
echo [步骤 7] 复制文档文件...
if exist "EULA-cn.txt" copy "EULA-cn.txt" "%DEPLOY_DIR%\" >nul
if exist "EULA-en.txt" copy "EULA-en.txt" "%DEPLOY_DIR%\" >nul
if exist "License.txt" copy "License.txt" "%DEPLOY_DIR%\" >nul
if exist "README.md" copy "README.md" "%DEPLOY_DIR%\" >nul
echo [完成] 文档文件已复制

echo.
echo [步骤 8] 将部署文件复制回 build\bin\Release...
xcopy /y /s /q "%DEPLOY_DIR%\*" "build\bin\Release\" >nul
echo [完成] 部署文件已同步

echo.
echo [步骤 9] 检查 Inno Setup 编译器...
set "ISCC="
if exist "C:\Program Files (x86)\Inno Setup 6\ISCC.exe" (
    set "ISCC=C:\Program Files (x86)\Inno Setup 6\ISCC.exe"
)
if exist "C:\Program Files\Inno Setup 6\ISCC.exe" (
    set "ISCC=C:\Program Files\Inno Setup 6\ISCC.exe"
)

if "%ISCC%"=="" (
    echo [警告] 未找到 Inno Setup 编译器
    echo 请手动运行 LimeReader.iss 文件
    echo 或者下载安装 Inno Setup: https://jrsoftware.org/isinfo.php
    echo.
    echo 文件已准备就绪，位于: build\bin\Release\
    pause
    exit /b 0
)

echo [完成] 找到 Inno Setup: %ISCC%

echo.
echo [步骤 10] 编译安装包...
"%ISCC%" "LimeReader.iss"
if errorlevel 1 (
    echo [错误] 安装包编译失败！
    pause
    exit /b 1
)

echo.
echo ========================================
echo [成功] 安装包制作完成！
echo ========================================
echo.
echo 输出位置: installer\LimeReader_Setup_v2.7.2.exe
echo.

:: 检查安装包是否存在
if exist "installer\LimeReader_Setup_v2.7.2.exe" (
    echo 安装包大小:
    dir "installer\LimeReader_Setup_v2.7.2.exe" | find "LimeReader"
    echo.
    echo 是否打开安装包所在文件夹？(Y/N)
    choice /c YN /n /m "请选择: "
    if errorlevel 2 goto :end
    if errorlevel 1 explorer "installer"
)

:end
echo.
pause

